#!/usr/bin/python2.6
#
# Copyright 2012 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time

def flock_test():
  """Method to run a net-score.org flock test and print out the result URL.
  """

  browser = webdriver.Chrome()
  browser.get('http://www.net-score.org')
  assert 'net-score' in browser.title

  browser.find_element_by_id('goButton').click()
  time.sleep(1)

  try:
      con_type = browser.find_element_by_xpath('//input[@label=\'Connection type...\']')
      con_type.send_keys('Wired LAN' + Keys.ENTER)
      time.sleep(1) 
      con_type.send_keys(Keys.ENTER)
  except NoSuchElementException:
      assert 0, 'connection type not found'
  time.sleep(30)

  while True:
    try:
        link = browser.find_element_by_xpath('//a[contains(@href,\'/user/detail/\')]')
        link_href = link.get_attribute('href')
        print link_href
        break
    except NoSuchElementException:
      time.sleep(10)
      continue
  browser.close()

def main():
  flock_test()

if __name__ == "__main__":
  main()
